package com.znz.analysis.spark.sparkContext;

import javax.annotation.Resource;

import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.springframework.stereotype.Component;

@Component
public class SparkContext {
	// JavaSparkContext对象封装
	private JavaSparkContext context;

	@Resource
	private SparkConfFactory sparkConfFactory;

	public JavaSparkContext getContext() {
		return context;
	}

	public void setContext(JavaSparkContext context) {
		this.context = context;
	}

	/**
	 * 初始化创建SparkContxt对象
	 */
	public void create() {
		context = new JavaSparkContext(sparkConfFactory.getInstance());
		context.setLogLevel("WARN");
	}

	/**
	 * 关闭SparkContxt
	 */
	public void close() {
		// 关闭数据库连接
		this.getContext().close();
	}

	/**
	 * 读取文件
	 * 
	 * @param path
	 * @return
	 */
	public JavaRDD<String> textFile(String path) {
		return this.getContext().textFile(path, 16);
	}
}
